﻿#pragma once
#include <iostream>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/strand.hpp>
#include "ws_io_interface.h"

namespace ws_
{
    namespace beast = boost::beast;         // from <boost/beast.hpp>
    namespace http = beast::http;           // from <boost/beast/http.hpp>
    namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
    namespace net = boost::asio;            // from <boost/asio.hpp>
    using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>


    class session : public std::enable_shared_from_this<session>
    {
        boost::beast::websocket::stream<beast::tcp_stream> ws_;
        boost::beast::flat_buffer rxBuffer_, txBuffer_;
        WsIoInterface* pp;

    public:
        // Take ownership of the socket
        explicit
            session(tcp::socket&& socket)
            : ws_(std::move(socket))
        {
            t1 = time(NULL);
            //	std::cout << "make session :" << socket.remote_endpoint().address() << std::endl;
        }
        void run(WsIoInterface* p);
        void on_run(void);
        void on_accept(boost::beast::error_code ec);
        void recvAsync(void);
        void sendAsync(std::string tmp);
        void do_RxTx(void);
        void on_read(boost::beast::error_code ec, std::size_t bytes_transferred);
        void on_write(boost::beast::error_code ec, std::size_t bytes_transferred);
        bool wbusy = false;
        bool session_valid = false;
        bool isFirst = false;
        time_t t1;
    };

    //client impliention
    class  ws_client
    {
    public:
        std::shared_ptr<session> session_sptr;
        WsIoInterface io;

    };
    //export class ws_client;
    //notice




    //void bindApi(WsIoInterface* p);
}

void startServer(const char* addr, uint16_t port, std::vector<ws_::ws_client*> *vp);